import {Component, OnDestroy, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {Subject} from 'rxjs';
import {MineService} from "@mine/mine.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {ToolsService} from "@shared/service/tools/tools.service";

import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {FeedRespVO} from "@interface/index";


@Component({
  selector: 'digg',
  templateUrl: 'digg.component.html',
  styleUrls: ['digg.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class DiggComponent implements OnInit, OnDestroy {
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public feeds: Array<FeedRespVO> = [];
  public stateStore = {
    haveData: true
  };
  private _params = {
    userId: '',
    page: 1,
    rows: 10
  };
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(public tools: ToolsService,
              public authSer: AuthService,
              public mineSer: MineService) {
    this._params.userId = this.authSer.getUserId();
  }

  /**
   * @description 获取我点赞过的帖子
   * @author ouyqx-691
   * @date 2018/1/25
   */
  getMyDiggFeeds() {
    this.tools.showLoading();
    this.mineSer.getMyDigg(this._params).takeUntil(this.ngUnsubscribe)
      .subscribe(res => {
        this.tools.hideLoading();
        if (res.result === '0') {
          this._params.page++;
          this.infiniteScroll.complete();
          const feeds = res.data.list;
          if (!res.data.hasNextPage) {
            if (feeds.length === 0) {
              this.stateStore.haveData = false;
            }
            this.infiniteScroll.enable(false);
          }
          this.feeds = [...this.feeds, ...feeds];
        }
      });
  }


  ngOnInit() {
    this.getMyDiggFeeds();
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
